package com.hskn.hss.module.eventtoremind.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.eventtoremind.entity.EventToRemind;
import com.hskn.hss.module.eventtoremind.mapper.EventToRemindMapper;
import com.hskn.hss.module.eventtoremind.service.IEventToRemindService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 一张网提醒设置表 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-24
 */
@Service
public class EventToRemindServiceImpl extends ServiceImpl<EventToRemindMapper, EventToRemind> implements IEventToRemindService {

    @Resource
    private EventToRemindMapper eventToRemindMapper;

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(uid)) {
            throw new HssException("参数错误");
        }
        EventToRemind eventToRemind = JSON.parseObject(params.toJSONString(), EventToRemind.class);
        UpdateWrapper<EventToRemind> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("create_uid", uid);
        eventToRemindMapper.update(eventToRemind, updateWrapper);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        EventToRemind eventToRemind = JSON.parseObject(params.toJSONString(), EventToRemind.class);
        eventToRemind.setCreateUid(uid);
        eventToRemindMapper.insert(eventToRemind);
    }

    @Override
    public EventToRemind detail(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(uid)) {
            throw new HssException("参数错误");
        }
        QueryWrapper<EventToRemind> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EventToRemind::getCreateUid, uid);
        EventToRemind eventToRemind = eventToRemindMapper.selectOne(queryWrapper);
        return eventToRemind;
    }
}
